package com.softsec.isc.analysis;

import java.io.DataInputStream;
import java.io.IOException;

public class DexHeader {
	
	public static void readDex(String fileName) throws IOException{
		
		int oct=0;
		int flag=GetFinal.getFlag(fileName);
		byte[] bufferArray = new byte[4];

		DataInputStream input2=apkExtractor.getDexDataInputStreamWithBuffered(fileName);
		
		if(flag==1){
			oct = Integer.parseInt("34", 16);
			input2.skip(oct);
			input2.read(bufferArray);

			int mapAddress=GetFinal.GetSixteen(bufferArray, flag);  

			DataInputStream input=apkExtractor.getDexDataInputStreamWithBuffered(fileName);
			
			input.skip(mapAddress);   
			input.read(bufferArray);
			int NumDexMapItem=GetFinal.GetSixteen(bufferArray,flag);
			int i=1;
			byte[] array2=new byte[2];
			int size1=0;int offset1=0;
			for(i=1;i<=NumDexMapItem;i++){
				input.read(array2);
				int type=(int)(((array2[1]&0xff)<<8)|(array2[0]&0xff));
				switch(type){
				case PrivateFinal.kDexTypeHeaderItem://HeaderItem
					input.skip(2);
					input.read(bufferArray);
					size1=GetFinal.GetSixteen(bufferArray,flag);
					input.read(bufferArray);
					offset1=GetFinal.GetSixteen(bufferArray,flag);
					DexMapItemMapper.getInstance().addDexMapItemCount("kDexTypeHeaderItem", size1);
					DexMapItemMapper.getInstance().addDexMapItemOffset("kDexTypeHeaderItem", offset1);
					break;
				case PrivateFinal.kDexTypeStringIdItem://StringIdItem
					input.skip(2);
					input.read(bufferArray);
					size1=GetFinal.GetSixteen(bufferArray,flag);
					input.read(bufferArray);
					offset1=GetFinal.GetSixteen(bufferArray,flag);
					DexMapItemMapper.getInstance().addDexMapItemCount("kDexTypeStringIdItem", size1);
					DexMapItemMapper.getInstance().addDexMapItemOffset("kDexTypeStringIdItem", offset1);
					break;
				case PrivateFinal.kDexTypeTypeIdItem://TypeIdItem
					input.skip(2);
					input.read(bufferArray);
					size1=GetFinal.GetSixteen(bufferArray,flag);
					input.read(bufferArray);
					offset1=GetFinal.GetSixteen(bufferArray,flag);
					DexMapItemMapper.getInstance().addDexMapItemCount("kDexTypeTypeIdItem", size1);
					DexMapItemMapper.getInstance().addDexMapItemOffset("kDexTypeTypeIdItem", offset1);
					break;
				case PrivateFinal.kDexTypeProtoIdItem://ProtoIdItem
					input.skip(2);
					input.read(bufferArray);
					size1=GetFinal.GetSixteen(bufferArray,flag);
					input.read(bufferArray);
					offset1=GetFinal.GetSixteen(bufferArray,flag);
					DexMapItemMapper.getInstance().addDexMapItemCount("kDexTypeProtoIdItem", size1);
					DexMapItemMapper.getInstance().addDexMapItemOffset("kDexTypeProtoIdItem", offset1);
					break;
				case PrivateFinal.kDexTypeFieldIdItem://FieldIdItem
					input.skip(2);
					input.read(bufferArray);
					size1=GetFinal.GetSixteen(bufferArray,flag);
					input.read(bufferArray);
					offset1=GetFinal.GetSixteen(bufferArray,flag);
					DexMapItemMapper.getInstance().addDexMapItemCount("kDexTypeFieldIdItem", size1);
					DexMapItemMapper.getInstance().addDexMapItemOffset("kDexTypeFieldIdItem", offset1);
					break;
				case PrivateFinal.kDexTypeMethodIdItem://MethodIdItem
					input.skip(2);
					input.read(bufferArray);
					size1=GetFinal.GetSixteen(bufferArray,flag);
					input.read(bufferArray);
					offset1=GetFinal.GetSixteen(bufferArray,flag);
					DexMapItemMapper.getInstance().addDexMapItemCount("kDexTypeMethodIdItem", size1);
					DexMapItemMapper.getInstance().addDexMapItemOffset("kDexTypeMethodIdItem", offset1);
					break;
				case PrivateFinal.kDexTypeClassDefItem://ClassDefIdItem
					input.skip(2);
					input.read(bufferArray);
					size1=GetFinal.GetSixteen(bufferArray,flag);
					input.read(bufferArray);
					offset1=GetFinal.GetSixteen(bufferArray,flag);
					DexMapItemMapper.getInstance().addDexMapItemCount("kDexTypeClassDefItem", size1);
					DexMapItemMapper.getInstance().addDexMapItemOffset("kDexTypeClassDefItem", offset1);
					break;
				case PrivateFinal.kDexTypeMapList://TypeList			   
					input.skip(2);
					input.read(bufferArray);
					size1=GetFinal.GetSixteen(bufferArray,flag);
					input.read(bufferArray);
					offset1=GetFinal.GetSixteen(bufferArray,flag);
					DexMapItemMapper.getInstance().addDexMapItemCount("kDexTypeMapList", size1);
					DexMapItemMapper.getInstance().addDexMapItemOffset("kDexTypeMapList", offset1);
					break;
				case PrivateFinal.kDexTypeTypeList://TypeList			   
					input.skip(2);
					input.read(bufferArray);
					size1=GetFinal.GetSixteen(bufferArray,flag);
					input.read(bufferArray);
					offset1=GetFinal.GetSixteen(bufferArray,flag);
					DexMapItemMapper.getInstance().addDexMapItemCount("kDexTypeTypeList", size1);
					DexMapItemMapper.getInstance().addDexMapItemOffset("kDexTypeTypeList", offset1);
					break;
				case PrivateFinal.kDexTypeAnnotationSetRefList://TypeList			   
					input.skip(2);
					input.read(bufferArray);
					size1=GetFinal.GetSixteen(bufferArray,flag);
					input.read(bufferArray);
					offset1=GetFinal.GetSixteen(bufferArray,flag);
					DexMapItemMapper.getInstance().addDexMapItemCount("kDexTypeAnnotationSetRefList", size1);
					DexMapItemMapper.getInstance().addDexMapItemOffset("kDexTypeAnnotationSetRefList", offset1);
					break;
				case PrivateFinal.kDexTypeAnnotationSetItem://TypeList			   
					input.skip(2);
					input.read(bufferArray);
					size1=GetFinal.GetSixteen(bufferArray,flag);
					input.read(bufferArray);
					offset1=GetFinal.GetSixteen(bufferArray,flag);
					DexMapItemMapper.getInstance().addDexMapItemCount("kDexTypeAnnotationSetItem", size1);
					DexMapItemMapper.getInstance().addDexMapItemOffset("kDexTypeAnnotationSetItem", offset1);
					break;
				case PrivateFinal.kDexTypeClassDataItem://TypeList			   
					input.skip(2);
					input.read(bufferArray);
					size1=GetFinal.GetSixteen(bufferArray,flag);
					input.read(bufferArray);
					offset1=GetFinal.GetSixteen(bufferArray,flag);
					DexMapItemMapper.getInstance().addDexMapItemCount("kDexTypeClassDataItem", size1);
					DexMapItemMapper.getInstance().addDexMapItemOffset("kDexTypeClassDataItem", offset1);
					break;
				case PrivateFinal.kDexTypeCodeItem://CodeIdItem
					input.skip(2);
					input.read(bufferArray);
					size1=GetFinal.GetSixteen(bufferArray,flag);
					input.read(bufferArray);
					offset1=GetFinal.GetSixteen(bufferArray,flag);
					DexMapItemMapper.getInstance().addDexMapItemCount("kDexTypeCodeItem", size1);
					DexMapItemMapper.getInstance().addDexMapItemOffset("kDexTypeCodeItem", offset1);
					break;			   
				case PrivateFinal.kDexTypeStringDataItem://TypeList			   
					input.skip(2);
					input.read(bufferArray);
					size1=GetFinal.GetSixteen(bufferArray,flag);
					input.read(bufferArray);
					offset1=GetFinal.GetSixteen(bufferArray,flag);
					DexMapItemMapper.getInstance().addDexMapItemCount("kDexTypeStringDataItem", size1);
					DexMapItemMapper.getInstance().addDexMapItemOffset("kDexTypeStringDataItem", offset1);
					break;
				case PrivateFinal.kDexTypeDebugInfoItem://TypeList			   
					input.skip(2);
					input.read(bufferArray);
					size1=GetFinal.GetSixteen(bufferArray,flag);
					input.read(bufferArray);
					offset1=GetFinal.GetSixteen(bufferArray,flag);
					DexMapItemMapper.getInstance().addDexMapItemCount("kDexTypeDebugInfoItem", size1);
					DexMapItemMapper.getInstance().addDexMapItemOffset("kDexTypeDebugInfoItem", offset1);
					break;
				case PrivateFinal.kDexTypeAnnotationItem://TypeList			   
					input.skip(2);
					input.read(bufferArray);
					size1=GetFinal.GetSixteen(bufferArray,flag);
					input.read(bufferArray);
					offset1=GetFinal.GetSixteen(bufferArray,flag);
					DexMapItemMapper.getInstance().addDexMapItemCount("kDexTypeAnnotationItem", size1);
					DexMapItemMapper.getInstance().addDexMapItemOffset("kDexTypeAnnotationItem", offset1);
					break;
				case PrivateFinal.kDexTypeEncodedArrayItem://TypeList			   
					input.skip(2);
					input.read(bufferArray);
					size1=GetFinal.GetSixteen(bufferArray,flag);
					input.read(bufferArray);
					offset1=GetFinal.GetSixteen(bufferArray,flag);
					DexMapItemMapper.getInstance().addDexMapItemCount("kDexTypeEncodedArrayItem", size1);
					DexMapItemMapper.getInstance().addDexMapItemOffset("kDexTypeEncodedArrayItem", offset1);
					break;
				case PrivateFinal.kDexTypeAnnotationDirectoryItem://TypeList			   
					input.skip(2);
					input.read(bufferArray);
					size1=GetFinal.GetSixteen(bufferArray,flag);
					input.read(bufferArray);
					offset1=GetFinal.GetSixteen(bufferArray,flag);
					DexMapItemMapper.getInstance().addDexMapItemCount("kDexTypeAnnotationDirectoryItem", size1);
					DexMapItemMapper.getInstance().addDexMapItemOffset("kDexTypeAnnotationDirectoryItem", offset1);
					break;		
				default:
					break;
				}
				
			}
			
		
			input.close();
			
		}
	
		input2.close();
	}
	
}